﻿using System;
using System.Linq;
using YuYuKiCms.Dal.Interface;
using YuYuKiCms.Dal.Model;

namespace YuYuKiCms.Dal.Repository
{
    internal class ThemeRepository : BaseRepository<Theme>, IThemeRepository
    {
        public ThemeRepository(IYuyukiModelUnitOfWork db)
            : base(db)
        {
        }

        /// <summary>
        ///     Get default theme
        /// </summary>
        /// <returns></returns>
        public Theme GetDefaultTheme()
        {
            return Db.Themes.FirstOrDefault(theme => theme.IsSelected)
                ?? Db.Themes.FirstOrDefault(theme => theme.Id == 1);
        }

        /// <summary>
        ///     Retrieve theme from path
        ///     Null if not found
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public Theme GeThemeFromPath(string path)
        {
            return Db.Themes
                .FirstOrDefault(theme => theme.Path.Equals(path, StringComparison.InvariantCultureIgnoreCase));
        }

        public bool IsThemeExistsFromPathView(string path)
        {
            return Db.Themes
                .Any(theme => theme.PathView.Equals(path, StringComparison.InvariantCultureIgnoreCase));
        }

        /// <summary>
        // Invalid all themes execept Default (Id=1)
        /// </summary>
        public void InvalidAllThemeExceptCurrentAndDefault()
        {
            Db.Themes.Where(f => f.Id == 1 || f.IsSelected)
                .ToList()
                .ForEach(theme => theme.IsPathValid = false);
        }
    }
}